<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\UpdateWechatMenu;
use App\Business\BLL\Wechat\Official;
use App\Models\WechatMenu;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Routing\Controller;

class WechatMenuController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('微信菜单')
            ->description(trans('admin.list'))
            ->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $wechatMenu = new WechatMenu;
                    $form = new \Encore\Admin\Widgets\Form();

                    $form->action(admin_base_path('wechatmenu'));

                    $form->select('parent_id', trans('admin.parent_id'))->options(WechatMenu::selectRoot());
                    $form->text('name', '名称')->rules('required');
                    $form->select('type', '类型')->options($wechatMenu->typeArray())->rules('required');
                    $form->text('url', '网址')->rules('required_if:type,view');
                    $form->text('appid', '小程序AppId')->rules('required_if:type,miniprogram');
                    $form->text('pagepath', '小程序首页')->rules('required_if:type,miniprogram');

                    $form->hidden('_token')->default(csrf_token());

                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
    }

    /**
     * Redirect to edit page.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function show($id)
    {
        return redirect()->route('menu.edit', ['id' => $id]);
    }

    /**
     * @return \Encore\Admin\Tree
     */
    protected function treeView()
    {

        return WechatMenu::tree(function (Tree $tree) {
            $tree->disableCreate();
            $tree->tools(function ($tools) {
                $tools->add('');
                $tools->add(new UpdateWechatMenu());
            });
            $wechatMenu = new WechatMenu;

            $tree->branch(function ($branch) use ($wechatMenu) {
                $payload = "<strong>{$branch['name']}</strong>";

                $payload .= "--<span>{$wechatMenu->typeArray($branch['type'])}</span>";

                return $payload;
            });
        });
    }

    /**
     * Edit interface.
     *
     * @param string  $id
     * @param Content $content
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('微信菜单')
            ->description(trans('admin.edit'))
            ->row($this->form()->edit($id));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $wechatMenu = new WechatMenu;
        // dd($wechatMenu->type());
        $form = new Form(new WechatMenu());
        $form->display('id', 'ID');

        $form->select('parent_id', trans('admin.parent_id'))->options(WechatMenu::selectRoot());
        $form->text('name', '名称')->rules('required');

        $form->select('type', '类型')->options($wechatMenu->typeArray())->rules('required');
        $form->text('url', '网址')->rules('required_if:type,view');
        $form->text('appid', '小程序AppId')->rules('required_if:type,miniprogram');
        $form->text('pagepath', '小程序首页')->rules('required_if:type,miniprogram');

        $form->display('created_at', trans('admin.created_at'));
        $form->display('updated_at', trans('admin.updated_at'));

        return $form;
    }

    public function publish()
    {
        //获取结构
        $list = WechatMenu::orderBy('order')->get();
        $parent = $list->where('parent_id', 0);
        $menu = [];
        foreach ($parent as $item) {
            if ($item->type == 'p') {
                //查找下一级
                $child = $list->where('parent_id', $item->id);
                $subMenu = [];
                foreach ($child as $vo) {
                    $subMenu[] = $this->setItem($vo);
                }
                $menu[] = [
                    'name' => $item->name,
                    'sub_button' => $subMenu,
                ];
            } else {
                $menu[] = $this->setItem($item);
            }
        }
        $official = Official::make();
        $res = $official->menu->create($menu);

        $ret = [
            'status' => true,
            'message' => '',
        ];
        return response()->json($ret);

    }

    private function setItem($item)
    {
        $array = [];
        switch ($item->type) {
            case 'miniprogram':
                $array = [
                    "type" => $item->type,
                    "name" => $item->name,
                    "url" => "http://mp.weixin.qq.com",
                    "appid" => $item->appid,
                    "pagepath" => $item->pagepath,
                ];
                break;
            case 'view':
                $array = [
                    "type" => $item->type,
                    "name" => $item->name,
                    "url" => $item->url,
                ];
                break;
            default:
                break;
        }
        return $array;
    }

}
